/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Processing;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 *
 */
public abstract class GameController
{
    protected Board board;
    protected Hashtable PawnList = new Hashtable();
    protected int m_ChessType = -1;
    protected int m_BoardWidth = 0;
    protected int m_BoardHeight = 0;
    protected int m_SquareSize = 0;
    protected int m_SquareNumber = 0;
    protected int m_NextImageIndex = 0;
    protected int m_SelectedImageIndex = 0;
    protected int MovablePawn = -1;
    protected int m_CollumnSquareNum = -1;
    protected int m_RowSquareNum = -1;
    protected boolean m_Instance = false;

    public abstract void InitController(int qcDiTruoc);
    public abstract Hashtable[] PlaceAMove(int vtDau, int vtCuoi, int loaiPhong);
    public abstract Hashtable[] PlaceAMove(int vtDau, int vtCuoi);
    public abstract void LoadState(byte[] dl);

    public int GetChessType()
    {
        return m_ChessType;
    }

    public boolean GetInstance()
    {
        return m_Instance;
    }

    public int GetNextImageIndex()
    {
        return m_NextImageIndex;
    }

    public int GetSelectedImageIndex()
    {
        return m_SelectedImageIndex;
    }

    public int GetCollumnSquareNum()
    {
        return m_CollumnSquareNum;
    }

    public int GetRowSquareNum()
    {
        return m_RowSquareNum;
    }

    public int GetSquareSize()
    {
        return m_SquareSize;
    }

    public int GetSquareNumber()
    {
        return m_SquareNumber;
    }

    public int GetBoardWidth()
    {
        return m_BoardWidth;
    }

    public int GetBoardHeight()
    {
        return m_BoardHeight;
    }

    public Hashtable GetPawnList()
    {
        return PawnList;
    }

    public int[] CompMove()
    {
        return board.CompMove();
    }

    public boolean Win()
    {
        return board.Win();
    }

    public int ConvertXY2VT(int x, int y)
    {
        return (y * m_SquareNumber + x);
    }

    protected int isPawn(int qc)
    {
        return board.isPawn(qc);
    }

    public int isKing(int qc)
    {
        return board.isKing(qc);
    }

    public byte[] ExportCurrentState()
    {
        byte[] kq = board.ExportBoard();

        kq[0] = (byte)MovablePawn;

        return kq;
    }

    public Hashtable[] GetMovableList(int vtQuanCo)
    {
        if (!PawnList.containsKey(new Integer(vtQuanCo)))
            return null;
        int pawn = ((Integer)PawnList.get(new Integer(vtQuanCo))).intValue();
        if (MovablePawn != isPawn(pawn))
            return null;
        Vector movebaleList = board.MovableList(vtQuanCo);
        if (movebaleList == null)
            return null;

        Hashtable[] result = new Hashtable[3];
        result[0] = new Hashtable();
        result[1] = new Hashtable();
        result[2] = new Hashtable();
        result[2].put(new Integer(vtQuanCo), PawnList.get(new Integer(vtQuanCo)));

        Vector temp = new Vector();
        for (int i = 0; i < movebaleList.size() - 2; ++i)
        {
            if (PawnList.containsKey(movebaleList.elementAt(i)))
            {
                result[1].put(movebaleList.elementAt(i), PawnList.get(movebaleList.elementAt(i)));
            }
            temp.addElement(movebaleList.elementAt(i));
        }

        result[1].put("dsNC", temp);

        Enumeration en = PawnList.keys();
        while(en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            if (!result[1].containsKey(each) && each.intValue() != vtQuanCo)
                result[0].put(each, PawnList.get(each));
        }

        return result;
    }

    public int MaxUndoTurn()
    {
        return board.MaxUndoTurns();
    }

    public Hashtable[] UndoAMove()
    {
        int[] undoResult = board.UndoAMove();
        if (undoResult == null)
            return null;

        Hashtable[] result = new Hashtable[3];
        result[0] = new Hashtable();
        result[1] = new Hashtable();
        result[2] = new Hashtable();

        PawnList.remove(new Integer(undoResult[3]));
        if (undoResult[4] != 0)
            PawnList.put(new Integer(undoResult[3]), new Integer(undoResult[4]));

        Enumeration en = PawnList.keys();
        while(en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            result[0].put(each, PawnList.get(each));
        }
        PawnList.put(new Integer(undoResult[0]), new Integer(undoResult[1]));

        result[1].put(new Integer(0), new Integer(undoResult[3]));
        result[1].put(new Integer(1), new Integer(undoResult[0]));
        result[1].put(new Integer(2), new Integer(undoResult[1]));

        MovablePawn = (MovablePawn == 0) ? 1 : 0;

        return result;
    }
}
